﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoBat
{
    public partial class MainForm : Form
    {
        private BatStatus curBatSataus = BatStatus.NONE;

        private Process curProcess = new Process();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            InitInfo();
        }

        private void InitInfo()
        {
            curProcess.OutputDataReceived -= new DataReceivedEventHandler(ProcessOutDataReceived);
            ProcessStartInfo p = new ProcessStartInfo();
            p.FileName = "cmd.exe";
            //p.Arguments = " -t 192.168.1.103";
            p.UseShellExecute = false;
            p.WindowStyle = ProcessWindowStyle.Hidden;
            p.CreateNoWindow = true;
            p.RedirectStandardError = true;
            p.RedirectStandardInput = true;
            p.RedirectStandardOutput = true;
            curProcess.StartInfo = p;
            curProcess.Start();

            curProcess.BeginOutputReadLine();
            curProcess.OutputDataReceived += new DataReceivedEventHandler(ProcessOutDataReceived);
        }

        /// <summary>
        /// 开始命令行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtCommand.Text.Trim()))
            {
                MessageBox.Show("请输入命令");
            }
            if (curBatSataus != BatStatus.ON)
            {
                curProcess.StandardInput.WriteLine(this.txtCommand.Text.Trim());
                curBatSataus = BatStatus.ON;
            }
            else
            {
                MessageBox.Show("当前进程正在运行，请先关闭");
            }

        }

        /// <summary>
        /// 结束命令行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {

            if (curBatSataus == BatStatus.ON)
            {
                curProcess.CancelOutputRead();//取消异步操作
                curProcess.Kill();
                curBatSataus = BatStatus.OFF;
                //如果需要手动关闭，则关闭后再进行初始化
                InitInfo();
            }
        }

        /// <summary>
        /// 进程接受事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ProcessOutDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (this.txtOutPutInfo.InvokeRequired)
            {
                this.txtOutPutInfo.Invoke(new Action(() =>
                {
                    this.txtOutPutInfo.AppendText(e.Data + "\r\n");
                }));
            }
            else {
                this.txtOutPutInfo.AppendText(e.Data + "\r\n");
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(this.curProcess.StartInfo.FileName) || this.curProcess.StandardInput.BaseStream.CanWrite) && curBatSataus != BatStatus.OFF)
            {
                curBatSataus = BatStatus.OFF;
            }
        }

        private void SystemCleanUp_Click(object sender, EventArgs e)
        {
            try
            {
                string targetDir = String.Format(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+ @"\SIT系统清理工具.bat");

                if (curBatSataus != BatStatus.ON)
                {
                    this.txtCommand.Text = "";
                    curProcess.StandardInput.WriteLine(targetDir);
                    curBatSataus = BatStatus.ON;
                }
                else
                {
                    MessageBox.Show("当前进程正在运行，请先关闭");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
            }
        }

        private void KillNoResponding_Click(object sender, EventArgs e)
        {
            this.txtCommand.Text = "taskkill.exe /F /FI \"status eq NOT RESPONDING\" ";
            if (curBatSataus != BatStatus.ON)
            {
                curProcess.StandardInput.WriteLine(this.txtCommand.Text.Trim());
                curBatSataus = BatStatus.ON;
            }
            else
            {
                MessageBox.Show("当前进程正在运行，请先关闭");
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            txtOutPutInfo.Clear();
             curProcess.CancelOutputRead();//取消异步操作
                curProcess.Kill();
                curBatSataus = BatStatus.OFF;
                //如果需要手动关闭，则关闭后再进行初始化
                InitInfo();
        }

        private void txtOutPutInfo_TextChanged(object sender, EventArgs e)
        {

        }


    }

    /// <summary>
    /// 命令状态
    /// </summary>
    public enum BatStatus {
        NONE = 0,
        ON = 1,
        OFF = 2
    }
}
